/*
 * TextReader.cpp
 *
 *  Created on: 2009/02/24
 *      Author: Yongshik Cho
 */

#include "TextReader.h"
#include "TileReader.h"
#include "RenderableReader.h"


const std::string TextReader::_TEXT_STYLE_TAG		= "TextStyle";
const std::string TextReader::_LINE_STYLE_TAG		= "LineStyle";
const std::string TextReader::_LINE_COLOR_TAG		= "LineColor";
const std::string TextReader::_FILL_STYLE_TAG		= "FillStyle";
const std::string TextReader::_FILL_COLOR_TAG		= "FillColor";
const std::string TextReader::_BACK_COLOR_TAG		= "BackColor";
const std::string TextReader::_TEXT_TAG				= "Text";

const std::string TextReader::_FONT_TAG				= "Font";
const std::string TextReader::_WIDTH_TAG			= "Width";
const std::string TextReader::_HEIGHT_TAG			= "Height";
const std::string TextReader::_ESCAPEMENT_TAG		= "Escapement";
const std::string TextReader::_ORIENTATION_TAG		= "Orientation";
const std::string TextReader::_WEIGHT_TAG			= "Weight";
const std::string TextReader::_ITALIC_TAG			= "Italic";
const std::string TextReader::_UNDERLINE_TAG		= "Underline";
const std::string TextReader::_STRIKEOUT_TAG		= "Strikeout";
const std::string TextReader::_OUTPRECISION_TAG		= "OutPrecision";
const std::string TextReader::_CLIPPRECISION_TAG	= "ClipPrecision";
const std::string TextReader::_QUALITY_TAG			= "Quality";
const std::string TextReader::_PITCHANDFAMILY_TAG	= "PitchAndFamily";
const std::string TextReader::_FACENAME_TAG			= "FaceName";

const std::string TextReader::_TEXT_BITMAP_TAG		= "TEXT_BITMAP";
const std::string TextReader::_TEXT_OUTLINE_TAG		= "TEXT_OUTLINE";
const std::string TextReader::_LINE_NONE_TAG		= "LINE_NONE";
const std::string TextReader::_LINE_SOLID_TAG		= "LINE_SOLID";
const std::string TextReader::_FILL_NONE_TAG		= "FILL_NONE";
const std::string TextReader::_FILL_SOLID_TAG		= "FILL_SOLID";


TextObject* TextReader::parse(TiXmlElement* element)
{
	TextObject* text = new TextObject();
	if(!text)
		AltoThrow(Exception::ERR_MEMORY);

	char* sTextStyle = (char*)element->Attribute(_TEXT_STYLE_TAG.c_str());
	char* sLineStyle = (char*)element->Attribute(_LINE_STYLE_TAG.c_str());
	char* sLineColor = (char*)element->Attribute(_LINE_COLOR_TAG.c_str());
	char* sFillStyle = (char*)element->Attribute(_FILL_STYLE_TAG.c_str());
	char* sFillColor = (char*)element->Attribute(_FILL_COLOR_TAG.c_str());
	char* sBackColor = (char*)element->Attribute(_BACK_COLOR_TAG.c_str());
	char* sText = (char*)element->Attribute(_TEXT_TAG.c_str());

	TiXmlElement* node = element->FirstChildElement(_TEXT_STYLE_TAG.c_str()); if(node) sTextStyle = (char*)node->GetText();
	node = element->FirstChildElement(_LINE_STYLE_TAG.c_str()); if(node) sLineStyle = (char*)node->GetText();
	node = element->FirstChildElement(_LINE_COLOR_TAG.c_str()); if(node) sLineColor = (char*)node->GetText();
	node = element->FirstChildElement(_FILL_STYLE_TAG.c_str()); if(node) sFillStyle = (char*)node->GetText();
	node = element->FirstChildElement(_FILL_COLOR_TAG.c_str()); if(node) sFillColor = (char*)node->GetText();
	node = element->FirstChildElement(_BACK_COLOR_TAG.c_str()); if(node) sBackColor = (char*)node->GetText();
	node = element->FirstChildElement(_TEXT_TAG.c_str()); if(node) sText = (char*)node->GetText();

	RenderableReader::parseRenderable(element, text);

	TCHAR wstr[512]; memset(wstr, 0, sizeof(wstr));

	if(sTextStyle)
	{
		if(sTextStyle == _TEXT_BITMAP_TAG)
			text->setTextStyle(TextObject::TEXT_BITMAP, false);
		else if(sTextStyle == _TEXT_OUTLINE_TAG)
			text->setTextStyle(TextObject::TEXT_OUTLINE, false);
	}
	
	if(sLineStyle)
	{
		if(sLineStyle == _LINE_NONE_TAG)
			text->setLineStyle(TextObject::LINE_NONE);
		else if(sLineStyle == _LINE_SOLID_TAG)
			text->setLineStyle(TextObject::LINE_SOLID);
	}

	uint32 color32;
	Color color;

	if(sLineColor)
	{
		sscanf(sLineColor, "%x", &color32);
		color = Color::fromARGB((float)((color32>>24)&0xFF)/255.f, (float)((color32>>16)&0xFF)/255.f,
			(float)((color32>>8)&0xFF)/255.f, (float)((color32>>0)&0xFF)/255.f);
		text->setLineColor(color);
	}

	if(sFillStyle)
	{
		if(sFillStyle == _FILL_NONE_TAG)
			text->setFillStyle(TextObject::FILL_NONE, false);
		else if(sFillStyle == _FILL_SOLID_TAG)
			text->setFillStyle(TextObject::FILL_SOLID, false);
	}

	if(sFillColor)
	{
		sscanf(sFillColor, "%x", &color32);
		color = Color::fromARGB((float)((color32>>24)&0xFF)/255.f, (float)((color32>>16)&0xFF)/255.f,
			(float)((color32>>8)&0xFF)/255.f, (float)((color32>>0)&0xFF)/255.f);
		text->setFillColor(color, false);
	}

	if(sBackColor)
	{
		sscanf(sBackColor, "%x", &color32);
		color = Color::fromARGB((float)((color32>>24)&0xFF)/255.f, (float)((color32>>16)&0xFF)/255.f,
			(float)((color32>>8)&0xFF)/255.f, (float)((color32>>0)&0xFF)/255.f);
		text->setBackColor(color, false);
	}


	//read font tag
	TiXmlElement* font = element->FirstChildElement(_FONT_TAG.c_str());
	if(font)
	{
		int32 width = 10, height = 8, escape = 0, orient = 0, weight = 400, outprec = 0, clipprec = 0, quality = 0, paf = 0;
		font->Attribute(_WIDTH_TAG.c_str(), &width);
		font->Attribute(_HEIGHT_TAG.c_str(), &height);
		font->Attribute(_ESCAPEMENT_TAG.c_str(), &escape);
		font->Attribute(_ORIENTATION_TAG.c_str(), &orient);
		font->Attribute(_WEIGHT_TAG.c_str(), &weight);
		char* sItalic = (char*)font->Attribute(_ITALIC_TAG.c_str());
		char* sUnderline = (char*)font->Attribute(_UNDERLINE_TAG.c_str());
		char* sStrikeout = (char*)font->Attribute(_STRIKEOUT_TAG.c_str());
		font->Attribute(_OUTPRECISION_TAG.c_str(), &outprec);
		font->Attribute(_CLIPPRECISION_TAG.c_str(), &clipprec);
		font->Attribute(_QUALITY_TAG.c_str(), &quality);
		font->Attribute(_PITCHANDFAMILY_TAG.c_str(), &paf);
		char* sFaceName = (char*)font->Attribute(_FACENAME_TAG.c_str());

		node = font->FirstChildElement(_WIDTH_TAG.c_str()); if(node) width = atoi(node->GetText());
		node = font->FirstChildElement(_HEIGHT_TAG.c_str()); if(node) height = atoi(node->GetText());
		node = font->FirstChildElement(_ESCAPEMENT_TAG.c_str()); if(node) escape = atoi(node->GetText());
		node = font->FirstChildElement(_ORIENTATION_TAG.c_str()); if(node) orient = atoi(node->GetText());
		node = font->FirstChildElement(_WEIGHT_TAG.c_str()); if(node) weight = atoi(node->GetText());
		node = font->FirstChildElement(_ITALIC_TAG.c_str()); if(node) sItalic = (char*)node->GetText();
		node = font->FirstChildElement(_UNDERLINE_TAG.c_str()); if(node) sUnderline = (char*)node->GetText();
		node = font->FirstChildElement(_STRIKEOUT_TAG.c_str()); if(node) sStrikeout = (char*)node->GetText();
		node = font->FirstChildElement(_OUTPRECISION_TAG.c_str()); if(node) outprec = atoi(node->GetText());
		node = font->FirstChildElement(_CLIPPRECISION_TAG.c_str()); if(node) clipprec = atoi(node->GetText());
		node = font->FirstChildElement(_QUALITY_TAG.c_str()); if(node) quality = atoi(node->GetText());
		node = font->FirstChildElement(_PITCHANDFAMILY_TAG.c_str()); if(node) paf = atoi(node->GetText());
		node = font->FirstChildElement(_FACENAME_TAG.c_str()); if(node) sFaceName = (char*)node->GetText();

		text->setWidth(width, false);
		text->setHeight(height, false);
		text->setEscapement(escape, false);
		text->setOrientation(orient, false);
		text->setWeight(weight, false);
		text->setOutPrecision(outprec, false);
		text->setClipPrecision(clipprec, false);
		text->setQuality(quality, false);
		text->setPitchAndFamily(paf, false);

		if(sItalic)
		{
			if(sItalic == TileReader::_TRUE_TAG)
				text->setItalic(true, false);
			else
				text->setItalic(false, false);
		}

		if(sUnderline)
		{
			if(sUnderline == TileReader::_TRUE_TAG)
				text->setUnderline(true, false);
			else
				text->setUnderline(false, false);
		}

		if(sStrikeout)
		{
			if(sStrikeout == TileReader::_TRUE_TAG)
				text->setStrikeout(true, false);
			else
				text->setStrikeout(false, false);
		}

		if(sFaceName)
#ifdef UNICODE
		{
			::MultiByteToWideChar(CP_UTF8, 0, sFaceName, (int32)strlen(sFaceName), wstr, 512);
			text->setFaceName(wstr, false);
		}
#else
			text->setFaceName(sFaceName, false);
#endif

	}

	if(sText)
#ifdef UNICODE
	{
		::MultiByteToWideChar(CP_UTF8, 0, sText, (int32)strlen(sText), wstr, 512);
		text->setText(wstr);
	}
#else
		text->setText(sText);
#endif

	//parse attachment
	RenderableReader::parseAttachment(element, text);
	//parse script
	RenderableReader::parseScript(element, text);

	return text;
}
